//
//  GZZZLViewController.m
//  Estate
//
//  Created by heyong on 16/6/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "GZZZLViewController.h"
#import "B_LouCell.h"
#import "TopTimeCell.h"
#import "B_ZLBottomCell.h"
#import "PositionTableCell.h"
#import "DefaineCell.h"
#import "PositionBlockModel.h"
#import "OrientationViewController.h"
@interface GZZZLViewController ()
{
    NSMutableArray *_titArr,*testArr;
    //工序
    NSMutableDictionary *_proDic;
}
// block参数
@property (strong, nonatomic) PositionBlockModel *blockModel;
// 当前被点的地址数据模型
@property (strong, nonatomic) AddressModel *addressModel;

@property(copy, nonatomic)NSString *danwei;//单位String
@property(copy, nonatomic)NSString *diqu;//地区string

@end


@implementation GZZZLViewController

- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}
- (NSMutableDictionary *)locationDic{
    if(_locationDic==nil){
        _locationDic=[NSMutableDictionary dictionary];
    }
    return _locationDic;
}
- (NSMutableArray *)weatherArr{
    if(!_weatherArr){
        _weatherArr=[NSMutableArray array];
    }
    return _weatherArr;
}
- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRECTS(0, 0, 375, 545) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[TopTimeCell class] forCellReuseIdentifier:@"toptime"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_LouCell.h" bundle:nil] forCellReuseIdentifier:@"louCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_ZLBottomCell" bundle:nil] forCellReuseIdentifier:@"dibuCell"];
        [_tableView registerClass:[PositionTableCell class] forCellReuseIdentifier:@"poCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"deCell"];
        
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:YES tabFooter:NO];
    }
    return _tableView;
}

- (Site_05 *)site{
    if(_site==nil){
        _site=[[Site_05 alloc]init];
    }
    return _site;
}
- (void)viewDidAppear:(BOOL)animated{
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
    [self setdata];
}

- (void)createView{
    //添加底部视图
    [self.view addSubview:self.tableView];
    _titArr=[[NSMutableArray alloc]initWithObjects:@"编号:", @"工程名称:",@"施工单位:",@"旁站监理人:",@"地区:",@"天气:",@"旁站的关键部位、工序:",@"卷材防水细部构造施工",nil];
    testArr=[LSUtility constructionUnitFromProId:PROID];
}

#pragma  mark - 设置数据
- (void)setdata{
    WEAKSELF
    _proDic=[NSMutableDictionary dictionary];
    if(_site!=nil){
        //天气数组
        NSMutableArray *wArr=DEF_PERSISTENT_GET_OBJECT(@"WEATHER");
        if(wArr.count>0){
            for (NSDictionary *wdic in wArr) {
                for (NSString *city in wdic) {
                    [self.weatherArr addObject:city];
                }
            }
            NSDictionary *dic=[wArr firstObject];
            NSString *deCity=[self.weatherArr firstObject];
            [weakSelf.dataDic setObject:deCity forKey:@"地区"];
            [weakSelf.dataDic setObject:dic[deCity] forKey:@"天气"];
        }

        [weakSelf.dataDic setObject:_site.danwei forKey:@"施工单位"];
        [weakSelf.dataDic setObject:_site.diqu forKey:@"地区"];
        //默认工序值
        [_proDic setObject:_site.lou forKey:@"楼"];
        [_proDic setObject:_site.ceng forKey:@"层"];
        [_proDic setObject:_site.duan forKey:@"段"];
        [_proDic setObject:_site.otherAddress forKey:@"关键工序"];
        //添加到字典
        [self.dataDic setObject:_proDic forKey:@"工序"];
        
        [weakSelf.locationDic setObject:_site.locationInfo forKey:@"locationInfo"];
        [weakSelf.locationDic setObject:_site.location forKey:@"location"];
        [weakSelf.locationDic setObject:_site.locationId forKey:@"locationId"];
        
    }else{
        //天气数组
        NSMutableArray *wArr=DEF_PERSISTENT_GET_OBJECT(@"WEATHER");
        if(wArr.count>0){
            for (NSDictionary *wdic in wArr) {
                for (NSString *city in wdic) {
                    [self.weatherArr addObject:city];
                }
            }
            NSDictionary *dic=[wArr firstObject];
            NSString *deCity=[self.weatherArr firstObject];
            weakSelf.site.diqu=deCity;
            weakSelf.site.weather=dic[deCity];
            [weakSelf.dataDic setObject:deCity forKey:@"地区"];
            [weakSelf.dataDic setObject:dic[deCity] forKey:@"天气"];
        }
        [weakSelf.dataDic setObject:@"" forKey:@"施工单位"];
    
        //默认工序值
        [_proDic setObject:@"" forKey:@"楼"];
        [_proDic setObject:@"" forKey:@"层"];
        [_proDic setObject:@"" forKey:@"段"];
        [_proDic setObject:@"" forKey:@"关键工序"];
        //添加到字典
        [self.dataDic setObject:_proDic forKey:@"工序"];
    }
    
}

#pragma mark-UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    }
    if(section==1){
        return 4;
    }else{
        return 3;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return 100*BIZ;
    }
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8*BIZ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *acell=@"acell";
    __weak typeof(self)weakSelf=self;
    //获得组数
    long section=indexPath.section;
    long row=indexPath.row;
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell];
    if(cell==nil){
        if(section==3){
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:acell];
        }else {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:acell];
        }
    }
    if(section==0){
        TopTimeCell *cell=[tableView dequeueReusableCellWithIdentifier:@"toptime" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.beginLab.text=[LSUtility TimeNow];
        //结束时间加五分钟
        cell.endLab.text=[LSUtility nowTimeAdd5Sec];
        [self.dataDic setObject:cell.beginLab.text forKey:@"开始时间"];
        cell.isEdit=self.isEdit;
        if(weakSelf.site.number!=nil){
            cell.beginLab.text=weakSelf.site.beginTime;
            cell.endLab.text=weakSelf.site.endTime;
            [self.dataDic setObject:cell.beginLab.text forKey:@"开始时间"];
            [self.dataDic setObject:cell.endLab.text forKey:@"结束时间"];
        }else{
            [self.dataDic setObject:cell.beginLab.text forKey:@"开始时间"];
            [self.dataDic setObject:cell.endLab.text forKey:@"结束时间"];
        }
        
        cell.clickTimeBlock=^(NSString *value){
            [self.dataDic setObject:value forKey:@"开始时间"];
        };
        cell.clickTimeBlock_end=^(NSString *value){
            [self.dataDic setObject:value forKey:@"结束时间"];
        };
        return cell;
        
    }else if(section==1){
        DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
        //设置标题
        cell.title.text=_titArr[indexPath.row];
        switch (row) {
            case 0://编号
                if(weakSelf.site.number!=nil){
                    cell.text.text=weakSelf.site.number;
                }else{
                    cell.text.text=@"";
                }
                break;
            case 1://工程名称
                cell.text.text=DEF_PERSISTENT_GET_OBJECT(@"proname");
                [self.dataDic setObject:cell.text.text forKey:@"工程名称"];
                break;
            case 2://施工单位
                if(weakSelf.site.danwei!=nil){
                    cell.text.text=weakSelf.site.danwei;
                    [self.dataDic setObject:weakSelf.site.danwei forKey:@"施工单位"];
                }else{
                    cell.text.text=@"请选择";
                }
                break;
            default://旁站监理人
                if(weakSelf.site.jlName!=nil){
                    cell.text.text=weakSelf.site.jlName;
                }else{
                    cell.text.text=DEF_PERSISTENT_GET_OBJECT(@"realname");
                }
                [self.dataDic setObject:cell.text.text forKey:@"旁站监理人"];
                break;
        }
        return cell;
        
    }else if(section==2){
        if(row==0){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
            
            cell.title.text=_titArr[indexPath.row+4];
            
            cell.text.text=weakSelf.site.diqu;
            [self.dataDic setObject:cell.text.text forKey:@"地区"];
            
            return cell;
            
        }else if (row==1){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
            
            cell.title.text=_titArr[indexPath.row+4];
            cell.text.text=weakSelf.site.weather;
            [self.dataDic setObject:cell.text.text forKey:@"天气"];
            return cell;
            
        }else{
            
            PositionTableCell *cell =[tableView dequeueReusableCellWithIdentifier:@"poCell" forIndexPath:indexPath];
            cell.isEdit=self.isEdit;

            if([self.locationDic objectForKey:@"locationInfo"]==nil||[[self.locationDic objectForKey:@"locationInfo"] isEqualToString:@""]){
                cell.btnStr=@"请选择位置";
            }else{
                NSDictionary *dic = [LSUtility dictionaryWithJsonString:self.locationDic[@"location"]];
                if(!dic){
                    cell.btnStr = self.locationDic[@"locationInfo"];
                }else {
                    cell.btnStr = [NSString stringWithFormat:@"%@ %@",dic[@"address"],dic[@"leftDistanceStr"]];
                }
            }

            
            cell.cliclWzBlock = ^{
                OrientationViewController *location=[[OrientationViewController alloc]init];
                location.flagStr = @"jilu";
                location.dataArr1 = [NSMutableArray arrayWithArray:self.blockModel.dataArr1];
                location.dataArr2 = [NSMutableArray arrayWithArray:self.blockModel.dataArr2];
                location.dataArr3 = [NSMutableArray arrayWithArray:self.blockModel.dataArr3];
                location.dataArr4 = [NSMutableArray arrayWithArray:self.blockModel.dataArr4];
                location.aID = self.blockModel.aID;
                location.firstID = self.blockModel.firstID;
                location.secondID = self.blockModel.secondID;
                location.thirdID = self.blockModel.thirdID;
                
                location.keyStr = self.blockModel.keyStr;
                location.number = self.blockModel.number;
                location.addressArr = [NSMutableArray arrayWithArray:self.blockModel.addressArr];
                location.position = self.blockModel.position;
                location.addressModel = self.addressModel;
                location.selectedX = self.blockModel.selectedX;
                location.selectedY = self.blockModel.selectedY;
                location.scale = self.blockModel.scale;
                
                if (location.number) {
                    location.hC = YES;
                }
                [self.navigationController pushViewController:location animated:YES];
                // 回传位置值
                location.returnPlaceInfoBlock = ^(PositionBlockModel *model, AddressModel *addModel, NSString *addInfoStr){
                    self.blockModel = model;
                    self.addressModel = addModel;
                    
                    [weakSelf.locationDic setObject:model.keyStr forKey:@"locationInfo"];
                    [weakSelf.locationDic setObject:addInfoStr forKey:@"location"];
                    [weakSelf.locationDic setObject:model.aID forKey:@"locationId"];
                    
                    [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:2 inSection:2]] withRowAnimation:UITableViewRowAnimationFade];
                    
                };
            };
            
            return  cell;

        }
    }else{
        if(indexPath.row==1){
            B_LouCell *cell=[tableView dequeueReusableCellWithIdentifier:@"louCell" forIndexPath:indexPath];
            cell.isEdit=self.isEdit;
            //楼
            cell.louChangeBlock=^(NSString *valueStr){
                weakSelf.site.lou=valueStr;
                [_proDic setObject:weakSelf.site.lou forKey:@"楼"];
            };
            if(weakSelf.site.lou !=nil){
                cell.lou.text=weakSelf.site.lou;
            }
            
            //层
            cell.cengChangeBlock=^(NSString *valueStr){
                weakSelf.site.ceng=valueStr;
                [_proDic setObject:weakSelf.site.ceng forKey:@"层"];
                
            };
            if(weakSelf.site.ceng !=nil){
                cell.ceng.text=weakSelf.site.ceng;
            }
            //段
            cell.duanChangeBlock=^(NSString *valueStr){
                weakSelf.site.duan=valueStr;
                [_proDic setObject:weakSelf.site.duan forKey:@"段"];
            };
            
            if(weakSelf.site.duan !=nil){
                cell.duan.text=weakSelf.site.duan;
                
            }
            cell. adderss.hidden=YES;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            return cell;
            
        }if(indexPath.row==2){
            B_ZLBottomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"dibuCell" forIndexPath:indexPath];
            cell.title.text=@"";
            cell.isEdit=self.isEdit;
            cell.textChangeBlock=^(NSString *valueStr){
                weakSelf.site.otherAddress=valueStr;
                [_proDic setObject:weakSelf.site.otherAddress forKey:@"关键工序"];
            };
            if(![weakSelf.site.otherAddress isEqualToString:@""]){
                cell.text.text=weakSelf.site.otherAddress;
                cell.text.placeholder=@"请填写关键工序";
            }
            //添加到字典
            [self.dataDic setObject:_proDic forKey:@"工序"];
            cell.title.text=@"";
            return cell;
            
        }else{
            
            cell.textLabel.text=_titArr[indexPath.row+6];
            
        }
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor=UIColorFromRGB(0x797979);
    cell.textLabel.textAlignment=0;
    cell.detailTextLabel.textColor=UIColorFromRGB(0x797979);
    cell.textLabel.font= [UIFont systemFontOfSize:13.];
    return cell;
}

#pragma  mark--点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];

    WEAKSELF
    if(self.isEdit){
        if(indexPath.section==1){
            if(indexPath.row==2){
                NSNumber *ninit = [NSNumber numberWithInteger:0];
                if(testArr.count<1){
                    [testArr addObject:@"无施工单位"];
                }
                [ActionSheetStringPicker showPickerWithTitle:nil rows:@[testArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                    //得到选择下标
                    int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                    weakSelf.site.danwei=testArr[iv];
                    
                    [self.dataDic setObject:weakSelf.site.danwei forKey:@"施工单位"];
                    [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                } cancelBlock:nil origin:self.view];
            }
        }
        if(indexPath.section==2){
            if(indexPath.row==0){
                NSNumber *ninit = [NSNumber numberWithInteger:0];
                NSMutableArray *wArr=DEF_PERSISTENT_GET_OBJECT(@"WEATHER");
                [ActionSheetStringPicker showPickerWithTitle:nil rows:@[self.weatherArr] initialSelection:@[ninit] doneBlock:^(ActionSheetStringPicker *picker, NSArray * selectedIndex, NSArray *selectedValue) {
                    int iv = [(NSNumber*)[selectedIndex firstObject] intValue];
                    
                    NSDictionary *weadic=wArr[iv];
                    NSString *address =self.weatherArr[iv];
                    
                    weakSelf.site.diqu=address;
                    weakSelf.site.weather=weadic[address];
                    NSIndexPath *weIndex=[NSIndexPath indexPathForRow:1 inSection:2];
                    
                    [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath,weIndex] withRowAnimation:UITableViewRowAnimationNone];

                } cancelBlock:nil origin:self.view];
                
            }
        }
        
    }else{
        NSLog(@"不可编辑");
    }
}

#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}


@end
